package com.ymttest.database.sqlwapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtProductsInLive;

import oracle.net.aso.s;

public class YmtProductsInLiveWapper {
	SqlManager manage;

	public YmtProductsInLiveWapper() {
		manage = new SqlManager("intpro");
	}
	
	
	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	/**
	 * 返回sqlmanager
	 * 
	 * @return
	 */
	public SqlManager getManager() {
		return manage;
	}

	
	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}
	
	
	
	public List<YmtProductsInLive> selectByLiveId(Integer liveId) throws InterruptedException{
		return manage.getSelectResults("com.ymttest.database.mapping.YmtProductsInLiveMapper.selectByLiveId", liveId);
	}
	
	//根据商品id查询
	public List<Map> selectInLiveByProductId(String productID) throws InterruptedException{
		return manage.getSelectResults("com.ymttest.database.mapping.YmtProductsInLiveMapper.selectInLiveByProductId", productID);
	}
	
	//根据商品id查询
		public List<Map> selectInLiveByProdIdAndLiveId(String productID,int iActivityId) throws InterruptedException{
			 Map<String, Object> m = new HashMap<String, Object>();
			 m.put("iActivityId", iActivityId);
			 m.put("sProductId", productID);
			return manage.getSelectResults("com.ymttest.database.mapping.YmtProductsInLiveMapper.selectInLiveByProdIdAndLiveId", m);
		}
		
	
	/**
	 * 查询商品在即将开始的直播中的，按照开始销售时间正序，最先开始的在第一个
	 * @param productID 要查询的商品ID
	 * @return 正行记录列表
	 * @throws Exception
	 */
	public List<YmtProductsInLive> selectInSonLiveByProductIdOrderBydStartTime(String productID) throws InterruptedException{
		return manage.getSelectResults("com.ymttest.database.mapping.YmtProductsInLiveMapper.selectInSonLiveByProductIdOrderBydStartTime", productID);
	}
	
	public List<Map> selectInLiveforcoutIstop(Integer liveId) throws InterruptedException{
		return manage.getSelectResults("com.ymttest.database.mapping.YmtProductsInLiveMapper.selectInLiveforcoutIstop", liveId);
	}
	
	//更新
	public int updateEndtimeByProductId(Date dEndTime , String productID) throws InterruptedException {
	    Map<String, Object> m = new HashMap<String, Object>();
	    m.put("dEndTime", dEndTime);
	    m.put("sProductId", productID);
	    int i = manage.update("com.ymttest.database.mapping.YmtProductsInLiveMapper.updateEndtimeByProductId", m);
	    manage.commit();
	    // manage.close();
	    return i;
	}
	
		public static void main(String args[]) {
		  try {
			System.out.println( new YmtProductsInLiveWapper().selectInLiveByProdIdAndLiveId("ff7859d2-67f7-4182-88e4-c74c8002752d", 157577).size());
	/*	         
			//查找productInActivityId
			YmtProductsInLiveWapper aa = new YmtProductsInLiveWapper();	
			List<Map> productsInActivity = aa.selectInLiveByProductId("05c3b40d-b90c-460e-bb40-b620a5736c78");
	//		Logger.debug(productsInActivity);
			Date dEndTime = (Date) productsInActivity.get(0).get("dStartTime");
			Logger.debug(dEndTime);
			int ret = aa.updateEndtimeByProductId(dEndTime,"05c3b40d-b90c-460e-bb40-b620a5736c78");
			Logger.debug(ret);*/
			
		    } catch (InterruptedException e) {
		        e.printStackTrace();
	    }

	}
}
